package com.sfy.auth.domain.service.impl;

import com.sfy.auth.common.enums.IsDeletedFlagEnum;
import com.sfy.auth.domain.entity.AuthRolePermissionBO;
import com.sfy.auth.domain.service.AuthRolePermissionDomainService;
import com.sfy.auth.infra.basic.entity.AuthRolePremission;
import com.sfy.auth.infra.basic.service.AuthRolePremissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (AuthRolePremission)表服务接口
 *
 * @author makejava
 * @since 2024-04-21 15:57:55
 */
@Service
public class AuthRolePermissionDomainServiceImpl implements AuthRolePermissionDomainService {

    @Resource
    private AuthRolePremissionService authRolePremissionService;

    //第一种方式（比较好性能）
//    @Override
//    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
//        Long roleId = authRolePermissionBO.getRoleId();
//        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
//            AuthRolePremission authRolePremission = new AuthRolePremission();
//            authRolePremission.setRoleId(roleId);
//            authRolePremission.setPermissionId(permissionId);
//            authRolePremission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
//            authRolePremissionService.insert(authRolePremission);
//        });
//        return true;
//    }

    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        List<AuthRolePremission> rolePremissionList = new ArrayList<>();
        Long roleId = authRolePermissionBO.getRoleId();
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            AuthRolePremission authRolePremission = new AuthRolePremission();
            authRolePremission.setRoleId(roleId);
            authRolePremission.setPermissionId(permissionId);
            authRolePremission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            rolePremissionList.add(authRolePremission);
        });
        Integer count = authRolePremissionService.batchInsert(rolePremissionList);
        return count > 0;
    }
}
